package com.hulushuju.core.net.http;

import com.hulushuju.core.net.ContentType;
import com.hulushuju.core.net.http.okhttp.OkHttpRequestImpl;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Hanjiafeng
 * @Date: 2019-05-28
 * @Desc: 请求抽象类
 */
public abstract class HttpRequest {
    @Getter
    private final Map<String, List<String>> headers = new HashMap<>();
    @Getter
    private final long builtTime = System.currentTimeMillis();
    @Getter
    private String url;
    @Getter
    private Method method;
    @Getter
    private ContentType contentType;
    @Getter
    private String formData;

    /**
     * 使用OkHttp初始化HttpRequest
     *
     * @return HttpRequest对象
     */
    public static HttpRequest getInstance() {
        return new OkHttpRequestImpl();
    }

    public HttpRequest url(String url) {
        this.url = url;
        return this;
    }

    //todo 稍后再写请求头的add,getByHost,set,remove方法

    public HttpRequest headers(Map<String, List<String>> headers) {
        this.headers.putAll(headers);
        return this;
    }

    public HttpRequest method(Method method) {
        this.method = method;
        return this;
    }

    public HttpRequest contentType(ContentType contentType) {
        this.contentType = contentType;
        return this;
    }

    public HttpRequest formData(String formData) {
        this.formData = formData;
        return this;
    }

    /**
     * 使用指定类型的类生成其对象
     *
     * @param c   类
     * @param <T> 类型
     * @return 实例化对象
     */
    public abstract <T> T convertRequest(Class<T> c);

    /**
     * 定义请求方法参数
     */
    @AllArgsConstructor
    public enum Method {
        /**
         * GET
         */
        GET("GET"),
        /**
         * POST
         */
        POST("POST");

        @Getter
        private String method;

        @Override
        public String toString() {
            return method;
        }
    }
}
